﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;

namespace jnxcLogin
{
    public partial class index : System.Web.UI.Page
    {
        Maticsoft.BLL.UserInfo um = new Maticsoft.BLL.UserInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["title"] != null)
                {
                    this.lbl_title.Text = Session["title"].ToString();
                    this.Page.Title = Session["title"].ToString();
                   
                }
                this.hid_loginsys.Value = "cb";
                HttpCookie cookies = Request.Cookies["USER_COOKIE"];
                if (cookies != null)
                {
                    //如果Cookie不为空，则将Cookie里面的用户名和密码读取出来赋值给前台的文本框。
                    this.txt_name.Text = cookies["UserName"];
                    this.txt_pass.Attributes.Add("value", cookies["UserPassword"]);
                    //这里依然把记住密码的选项给选中。
                    this.cbo_isremember.Checked = true;
                }
            }

        }

        //登录按钮
        protected void btn_login_Click(object sender, EventArgs e)
        {
            string name = this.txt_name.Text;
            string pass = this.txt_pass.Text;

            RemeberAccount();

            DataSet ds = um.GetList("loginName='" + name + "' and loginPass='" + pass + "'");

            string sysName = this.hid_loginsys.Value;
            #region 登录
            if (sysName == "cb")
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    bool isP = Convert.ToBoolean(ds.Tables[0].Rows[0]["sys_CB"]);

                    if (isP)
                    {
                        int sysId = Convert.ToInt32(ds.Tables[0].Rows[0]["sys_CB_uid"]);
                        string ipstr = ConfigurationManager.AppSettings["linkCBurl"];
                        Response.Redirect(ipstr + "LoginService.aspx?loginId=" + sysId);
                    }
                    else
                    {
                        Maticsoft.Common.MessageBox.Show(this.Page, "对不起，您没有登录测报系统的授权！");
                    }
                }
            }
            else if (sysName == "fz")
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    bool isP = Convert.ToBoolean(ds.Tables[0].Rows[0]["syc_FZ"]);
                    if (isP)
                    {
                        int sysId = Convert.ToInt32(ds.Tables[0].Rows[0]["sys_FZ_uid"]);
                        string ipstr = ConfigurationManager.AppSettings["linkFZurl"];
                        Response.Redirect(ipstr + "LoginService.aspx?loginId=" + sysId);
                    }
                    else
                    {
                        Maticsoft.Common.MessageBox.Show(this.Page, "对不起，您没有登录防治系统的授权！");
                    }

                }
            }
            else if (sysName == "jy")
            {

            }
            else if (sysName == "ks")
            {

            }
            else if (sysName == "sb")
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    bool isP = Convert.ToBoolean(ds.Tables[0].Rows[0]["sys_SB"]);
                    if (isP)
                    {
                        int sysId = Convert.ToInt32(ds.Tables[0].Rows[0]["sys_SB_uid"]);
                        string ipstr = ConfigurationManager.AppSettings["linkSBurl"];
                        Response.Redirect(ipstr + "LoginService.aspx?loginId=" + sysId);
                    }
                    else
                    {
                        Maticsoft.Common.MessageBox.Show(this.Page, "对不起，您没有登录设备监测系统的授权！");
                    }

                }
            }
            else
            {
                Maticsoft.Common.MessageBox.Show(this.Page, "请选择所要登录的系统!");
            }
            #endregion

            

        }

        
        protected void lbn_loginAdmin_Click(object sender, EventArgs e)
        {
            string name = this.txt_name.Text;
            string pass = this.txt_pass.Text;

            RemeberAccount();

            DataSet ds = um.GetList("loginName='" + name + "' and loginPass='" + pass + "'");
            if (ds.Tables[0].Rows.Count > 0)
            {
                Session["loginUser"] = ds;
                Response.Redirect("Frame.aspx");
            }
            else
            {
                Maticsoft.Common.MessageBox.Show(this.Page, "用户名密码不正确！");
            }
        }

        //记住密码
        private void RemeberAccount()
        {
            HttpCookie cookie = new HttpCookie("USER_COOKIE");
            if (this.cbo_isremember.Checked)
            {
                //所有的验证信息检测之后，如果用户选择的记住密码，则将用户名和密码写入Cookie里面保存起来。
                cookie.Values.Add("UserName", this.txt_name.Text.Trim());
                cookie.Values.Add("UserPassword", this.txt_pass.Text.Trim());
                //这里是设置Cookie的过期时间，这里设置一个星期的时间，过了一个星期之后状态保持自动清空。                    cookie.Expires = System.DateTime.Now.AddDays(7.0);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
            else
            {
                if (cookie["USER_COOKIE"] != null)
                {
                    //如果用户没有选择记住密码，那么立即将Cookie里面的信息情况，并且设置状态保持立即过期。
                    Response.Cookies["USER_COOKIE"].Expires = DateTime.Now;
                }
            }
        }

    }
}